package com.cmz.common.api.providers;

import com.cmz.common.enums.CommonError;
import com.cmz.common.exception.ServiceRuntimeException;
import com.cmz.common.utils.SpringUtils;

import javax.sql.DataSource;
import java.util.Map;

/**
 * 数据源提供者
 * @author cmz
 * @date 2020/6/5 11:04
 */
public interface DataSourceProvider {
    /**
     * 切换DataSource
     * @param id dataSource id, default: primary or main
     * @return dataSource
     */
    default DataSource switchDataSource(String id) {
        Map<String, DataSource> map = SpringUtils.getBeans(DataSource.class);
        return map.values().stream().findFirst().orElseThrow(() -> new ServiceRuntimeException(CommonError.XDBSDKCM0000));
    }
}
